#!/bin/bash

set +x

BASE_DIR=/app/oem/ai/positec
SRC_DIR=${BASE_DIR}/robot
LOG_DIR=${BASE_DIR}/robot_logs/update.log
SCRIPT_DIR=${BASE_DIR}/robot_update
TEMP_DIR=${SCRIPT_DIR}/.tmp
MISC_NAME=misc

MISC_DIR=${SRC_DIR}/${MISC_NAME}
TARGET_ARY=("src" "devel" "${MISC_NAME}")
DEST_FILE=tx2_update
KEY_FILE=key
VER_FILE=version

do_check_upfile()
{
    echo "do check update file" >> ${LOG_DIR}
    echo "do check update file"
    if [ ! -f ${SCRIPT_DIR}/${DEST_FILE}.tar.gz.enc ];then
        echo "update file NOT EXIST" >> ${LOG_DIR}
        echo "update file NOT EXIST"
        exit 0
    fi
}

do_check()
{
    echo "do_check:" >> ${LOG_DIR}
    for (( i = 0 ; i < ${#TARGET_ARY[@]} ; i++ ))
    do
        if [ ! -d ${SRC_DIR}/${TARGET_ARY[i]} ];then
            echo "${SRC_DIR}/${TARGET_ARY[i]} DOES NOT EXIST" >> ${LOG_DIR}
            echo "${SRC_DIR}/${TARGET_ARY[i]} DOES NOT EXIST"
            exit 0
        fi
    done

    if [ ! -f ${MISC_DIR}/${KEY_FILE} ];then
        echo "KEY DOES NOT EXIST" >> ${LOG_DIR}
        echo "KEY DOES NOT EXIST"
        exit 0
    fi

    if [ ! -f ${MISC_DIR}/${VER_FILE} ];then
        echo "VERSION DOES NOT EXIST" >> ${LOG_DIR}
        exit 0
    fi
    echo "SRC version: `cat ${MISC_DIR}/${VER_FILE}`" >> ${LOG_DIR}
    echo "SRC version: `cat ${MISC_DIR}/${VER_FILE}`"
}

do_clean()
{
    echo "do_clean:" >> ${LOG_DIR}
    echo "do_clean:"
    rm -rf ${TEMP_DIR} > /dev/null 2>&1
    for (( i = 0 ; i < ${#TARGET_ARY[@]} ; i++ ))
    do
        rm -rf ${SRC_DIR}/${TARGET_ARY[i]}.del > /dev/null 2>&1
        rm -rf ${SRC_DIR}/${TARGET_ARY[i]}.tmp > /dev/null 2>&1
    done
    sync
}

